// 深拷贝
export function deepClone<T>(source: T): T {
	return Array.isArray(source)
			? source.map((item) => deepClone(item))
		: source instanceof Date
					? new Date(source.getTime())
					: source && typeof source === 'object'
							? Object.getOwnPropertyNames(source).reduce((o, prop) => {
								Object.defineProperty(
										o,
										prop,
										Object.getOwnPropertyDescriptor(source, prop)!
								);
								o[prop] = deepClone((source as { [key: string]: any })[prop]);
								return o;
							}, Object.create(Object.getPrototypeOf(source)))
							: (source as T);
}

export function copyObjectWithInclusion(sourceObject:object, includedKeys:string[]){
	const targetObject = {};
	for (const key of includedKeys) {
		if (sourceObject.hasOwnProperty(key)) {
			targetObject[key] = sourceObject[key];
		}
	}
	return targetObject;
}


export function bindingBean(targetObj,copyObj){
	if(!targetObj||!copyObj)return
	for(const key in copyObj){
		if(targetObj.hasOwnProperty(key)){
			targetObj[key] = copyObj[key]
		}
	}
}


/**
 * 防抖
 * callBack回调函数里面如果要使用this不要用箭头函数
 */
export function debounce(callBack, time) {
	var timer = null
	return function (...args) {
		clearTimeout(timer)
		timer = setTimeout(() => {
			callBack.call(this, ...args)
		}, time)
	}
}

/**
 * 节流
 * callBack回调函数里面如果要使用this不要用箭头函数
 */

export function throttle(callBack, time) {
	var flag = true
	return function (...args) {
		if (flag) {
			setTimeout(() => {
				callBack.call(this, ...args)
				flag = true
			}, time)
		}
		flag = false
	}
}
/**
 * @description 格式化时间为字符串
 */
export function formatDateString(date, format = "yyyy-MM-dd HH:mm:ss") {
	if(typeof date === 'number') date = new Date(date)
	if (date instanceof Date) {
		const year = date.getFullYear().toString();
		const month = (date.getMonth() + 1).toString();
		const month2 = month.length > 1 ? month : "0" + month;
		const day = date.getDate().toString();
		const day2 = day.length > 1 ? day : "0" + day;
		const hours = date.getHours().toString();
		const hours2 = hours.length > 1 ? hours : "0" + hours;
		const minutes = date.getMinutes().toString();
		const minutes2 = minutes.length > 1 ? minutes : "0" + minutes;
		const seconds = date.getSeconds().toString();
		const seconds2 = seconds.length > 1 ? seconds : "0" + seconds;

		return format
				.replace("yyyy", year)
				.replace("MM", month2)
				.replace("M", month)
				.replace("dd", day2)
				.replace("d", day)
				.replace("HH", hours2)
				.replace("H", hours)
				.replace("mm", minutes2)
				.replace("m", minutes)
				.replace("ss", seconds2)
				.replace("s", seconds);
	}
}

/**
 * @description 时间如果再今天或者明天就不显示年月日
 */
export function getOptimizeDate(date) {
	if (typeof date === "string" || typeof date === "number") date = new Date(date)
	if (!(date instanceof Date)) return
	let hours = date.getHours()
	hours < 10 && (hours = "0" + hours)
	let minutes = date.getMinutes()
	minutes < 10 && (minutes = "0" + minutes)
	date = date.getTime()
	let str = ""
	const nowDate = new Date()
	nowDate.setHours(0)
	nowDate.setMinutes(0)
	nowDate.setSeconds(0)
	const todayStart = new Date(nowDate).getTime()
	const todayEnd = todayStart + 24 * 60 * 59 * 1000
	const tomorrowStart = todayEnd + 1000
	const tomorrowEnd = tomorrowStart + 24 * 60 * 59 * 1000
	if (date >= todayStart && date <= tomorrowEnd) {
		str = (date >= todayStart && date <= todayEnd ? "今天 " : "明天 ") + hours + ":" + minutes
	} else {
		str = formatDateString(date, "yyyy年MM月dd日 HH:mm")
	}
	return str
}
/**
 *  @description 对时间段进行操作
 */
export function getOptimizeDateTwo(date){
	if(!date) return ""
	if(date.includes("~")){
		date =  date.split("~")
		let temp = getOptimizeDate(date[1]).replace(/今天|明天|.*日/,'')
		return getOptimizeDate(date[0])+"~"+temp
	}else{
		return getOptimizeDate(date)
	}
}

/**
 *  获取整数倍时间
 */
export function getMinuteIntTime(minutes) {
	let maxHours = 0;
	let timeList = []
	do {
		const val = maxHours > 9 ? "" + maxHours : "0" + maxHours;
		let maxMinutes = 0;
		do {
			let val2 = maxMinutes > 9 ? "" + maxMinutes : "0" + maxMinutes;
			val2 = val + ":" + val2
			timeList.push(val2);
			maxMinutes = maxMinutes + minutes;
		} while (maxMinutes < 60);
		maxHours++;
	} while (maxHours < 24);
	return timeList
}
/**
 * 比较时间的大小
 * @param date1 123123
 * @param date2
 * @returns 1表示date1大，-1表示date1小 0表示相等
 */
export function compareDate(date1,date2){
	date1 = new Date(date1).getTime()
	date2 = new Date(date2).getTime()
	if(date1>date2) return 1
	if(date1==date2) return 0
	if(date1<date2) return -1
}

/**
 * @param {string} baseUrl 基本路径
 * @param {string} path 需要拼接在后面的路径
 * @return
 */

export function joinUrl(baseUrl, path) {
	// 如果基础 URL 以斜杠结尾，且路径以斜杠开头，则去除其中一个斜杠
	if (baseUrl.endsWith('/') && path.startsWith('/')) {
		return baseUrl + path.substring(1);
	}
	// 如果基础 URL 不以斜杠结尾，且路径不以斜杠开头，则添加斜杠
	if (!baseUrl.endsWith('/') && !path.startsWith('/')) {
		return baseUrl + '/' + path;
	}
	// 其他情况直接拼接
	return baseUrl + path;
}
